﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.Models;
using PBOffice.BLL;

public partial class NewsManager_NewsManager : System.Web.UI.Page
{
    List<News> newsList;
    string type;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            newsList = new NewsManager().GetAllNewsByType("");
            this.GridView1.DataSource = newsList;
            this.GridView1.DataBind();
            this.ddlNewsType.DataSource = new NewsTypeManager().GetAllType();
            this.ddlNewsType.DataTextField = "TypeName";
            this.ddlNewsType.DataBind();
            ListItem ll=new ListItem( "请选择");
            this.ddlNewsType.Items.Insert(0, ll);

        }
    }
    protected void btnCheck_Click(object sender, EventArgs e)
    {
        if (this.ddlNewsType.Text.Trim() != "请选择")
        {
            type = this.ddlNewsType.Text;
        }
        else
        {
            type = "";
        }
        newsList = new NewsManager().GetAllNewsByType(type);
        this.GridView1.DataSource = newsList;
        this.GridView1.DataBind();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string type = this.ddlNewsType.Text.Trim();
        if (this.ddlNewsType.Text=="请选择")
        {
            type = ""; 
        }
        int newsId = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[0].Text);
        new NewsManager().DeleteNews(newsId);
        newsList = new NewsManager().GetAllNewsByType(type);
        this.GridView1.DataSource = newsList;
        this.GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName=="modify")
        {
            GridViewRow dv = (GridViewRow)(((ImageButton)(e.CommandSource)).Parent.Parent);
            int newsId =Convert.ToInt32(GridView1.Rows[dv.RowIndex].Cells[0].Text);
            Response.Redirect(string.Format("AddNews.aspx?id={0}", newsId));
        }
    }
}